<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Image;
use Validator;
use DB;
use App\Models\Common;
class LunboController extends Controller
{
   

     //展示登录的用户城市轮播页
    public function index(Request $request)
    {
        
        //获取session中的对象 将对象存入变量$users
        
     //  $users_id = $request->session()->get('user_id');
       
     //  $city_id = DB::table('city')->select('id')->where('user_id',$users_id)->first();  ->where('city_id',$city_id->id)
      
       $users = DB::table('lunbo')->where('is_del',0)->get();
       
        return view('admin.lunbo.index',['users'=>$users]);
    }
    
       //浏览详情信息
    public function show($id)
    {
        return "详情".$id;
    }
    
    //添加表单
    public function create()
    {
        return view("admin.lunbo.add");
    }
    
    //添加轮播展示 登录那个地区的城市用户添加那个~~
    public function store(Request $request)
    {
      
      // $users_id = $request->session()->get('user_id');
     //  $city_id = DB::table('city')->select('id')->where('user_id',$users_id)->first();
       
         //获取指定的部分数据
         $data['sort'] = $request->input('sort');
        
            if(!$request->hasFile('pic')){
                 $request->session()->flash('error_msg','文件不存在');
                 return back();
             }
            $img = $request->file('pic');
            // 获取后缀名
            $ext = $img->extension();
            // 新文件名
            $saveName =time().rand().".".$ext;
            // 存储文件 已经不使用 move 这种方式
            // $img->move('./uploads/'.date('Ymd'),$saveName);
            // 使用 store 存储文件
            $data['banner'] = $img->store(date('Ymd'));
            $data['created_at'] = date("Y-m-d H:i:s");
      //      $data['city_id']  = $city_id->id;
            $id = \DB::table("lunbo")->insertGetId($data);
        
        if($id>0){
            return redirect('lunbo');
        }else{
           return back()->with("err","添加失败!");
        }
    }
    
    //执行删除
    public function destroy($id)
    {
        $info =  \DB::table('lunbo')
            ->where('id',$id)
            ->update(['is_del' => 1]);
    
            if($info){
                 return redirect("lunbo")->with("err","删除成功！");
            }else{
                 return redirect("lunbo")->with("err","删除失败！");
            }
    }
    
    //加载修改表单
    public function edit($id)
    {
        $users = \DB::table("lunbo")->where("id",$id)->first(); //获取要编辑的信息
        return view("admin.lunbo.edit",["vo"=>$users]);
    }
    
    //执行修改
    public function update($id,Request $request)
    {
        
           
          $data['sort'] = request('sort');
         
            if($request->hasFile('pic')){
                $img = $request->file('pic');
                // 获取后缀名
                $ext = $img->extension();
                // 新文件名
                $saveName =time().rand().".".$ext;
                // 存储文件 已经不使用 move 这种方式
                // $img->move('./uploads/'.date('Ymd'),$saveName);
                // 使用 store 存储文件
                $data['banner'] = $img->store(date('Ymd'));
               
             }else{
                 
                   $info = \DB::table("lunbo")->where("id",$id)->first();
                   $data['banner']  = $info->banner;
                   
               
             }
             
            
       
        
        $data['updated_at'] = date('Y-m-d H:i:s');
       
     
        $id = \DB::table("lunbo")->where("id",$id)->update($data);
        if($id>0){
            return redirect('lunbo');
        }else{
            return back()->with("err","修改失败!");
        }
        
    }

}
